package com.qishuihe.common.utils.threads;

import lombok.Data;

import java.util.concurrent.*;

/**
 * 全局线程池
 */
@Data
public class ThreadPoolTools  {

    /**
     * 线程池的核心线程数量
     */
    private  final int corePoolSize = 4;
    /**
     * 线程池的最大线程数，一般情况我们设计为容器CPU的一般数
     */
    private  final int maximumPoolSize = 6;
    /**
     * 线程在终止前可以保持空闲的时间限制。
     */
    private  final long keepAliveTime = 2;
    /**
     * 线程存活的时间单位
     */
    private  final TimeUnit unit = TimeUnit.SECONDS;
    /**
     * 执行前用于保持任务的队列数
     */
    private  final int QueueSize = 5;

    private static  volatile   ThreadPoolTools threadPoolTools;

    private  ThreadPoolExecutor threadPoolExecutor;

    private ThreadPoolTools(){

    }

    public static ThreadPoolTools   getThreadPoolTools(){
        if(null==threadPoolTools) {
            synchronized (ThreadPoolTools.class){
                threadPoolTools = new ThreadPoolTools() ;
                threadPoolTools.threadPoolExecutor = new ThreadPoolExecutor(threadPoolTools.corePoolSize,threadPoolTools.maximumPoolSize, threadPoolTools.keepAliveTime,threadPoolTools.unit,new ArrayBlockingQueue(threadPoolTools.QueueSize), new ThreadPoolExecutor.CallerRunsPolicy() );
            }
        }
        return threadPoolTools;
    }
    public ThreadPoolExecutor getThreadPoolExecutor(){
        return threadPoolExecutor;
    }




}
